package com.xiaogao.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaogao.autofill.annotation.FillUserInfo;
import com.xiaogao.common.entity.PageData;
import com.xiaogao.common.utils.BeanListUtils;
import com.xiaogao.sys.mapper.SysPostMapper;
import com.xiaogao.sys.model.SysPost;
import com.xiaogao.sys.request.post.AddParam;
import com.xiaogao.sys.request.post.QueryParam;
import com.xiaogao.sys.request.post.UpdateParam;
import com.xiaogao.sys.response.SysPostResponse;
import com.xiaogao.sys.service.SysPostService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements SysPostService {
    @Autowired
    private SysPostMapper sysPostMapper;

    @FillUserInfo
    @Override
    public PageData<SysPostResponse> querySelective(Integer pageNum, Integer pageSize, QueryParam queryParam) {
        // 1、先查ids
        LambdaQueryWrapper<SysPost> queryWrapper = getQueryWrapper(queryParam);
        // 分页
        Integer offset = (pageNum - 1) * pageSize;
        queryWrapper.last(String.format("limit %d,%d", offset, pageSize));
        // 只返回id列
        queryWrapper.select(SysPost::getId);
        List<String> ids = sysPostMapper.selectObjs(queryWrapper);
        if(CollectionUtils.isEmpty(ids)){
            return new PageData<>(new ArrayList<>(),0L);
        }
        // 2、根据ids查询sysPostResponseList
        List<SysPost> sysPosts = sysPostMapper.selectByIds(ids);
        List<SysPostResponse> sysPostResponses = BeanListUtils.copyList(sysPosts, SysPostResponse.class);
        // 3、查count
        LambdaQueryWrapper<SysPost> queryCountWrapper = getQueryWrapper(queryParam);
        Long total = sysPostMapper.selectCount(queryCountWrapper);
        return new PageData<>(sysPostResponses, total);
    }

    @Override
    public void add(AddParam addParam) {
        // 1、addParam to SysPost
        SysPost sysPost = new SysPost();
        BeanUtils.copyProperties(addParam, sysPost);
        sysPostMapper.insert(sysPost);
    }

    @Override
    public void update(UpdateParam updateParam) {
        // 1、updateParam to SysPost
        SysPost sysPost = new SysPost();
        BeanUtils.copyProperties(updateParam, sysPost);
        sysPostMapper.updateById(sysPost);
    }

    @Override
    public void delete(List<String> ids) {
        // 1、直接删除岗位
        sysPostMapper.deleteByIds(ids);
    }

    private LambdaQueryWrapper<SysPost> getQueryWrapper(QueryParam queryParam) {
        LambdaQueryWrapper<SysPost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(queryParam.getCode()), SysPost::getCode, queryParam.getCode());
        queryWrapper.like(StringUtils.isNotEmpty(queryParam.getName()), SysPost::getName, queryParam.getName());
        return queryWrapper;
    }
}
